% Written for NGRI - Styam Project 'Unified Presentation Framework'
% Converts the Spectra file to App & res
% writes to a text file
% with a format accepted by NGRI & Satyam
% Latest date 8.7.2003

function[] = satyam(SPM),

if isstruct(SPM) ~=1,
   return;
end;

for i = 1:length(SPM.spectra),
   ProcDef.TLFreq1(i) =SPM.spectra(i).freq;
end;

ProcDef.equations = [4 1 2;5 2 1];  % defining bi-variate eqns                  Local spec change here
ProcDef.nfrq=length(SPM.spectra);
ProcDef.neq = 2; % number of equtions defined
[tf]=transfn(SPM,ProcDef);
ProcDef.file1 = SPM.head.site;

frq=ProcDef.TLFreq1;
file=ProcDef.file1;
nfrq = length(frq);

for i = 1:nfrq,   
   Phxy(i) = atan2(imag(tf(i,1)),real(tf(i,1))) * 180 / 3.1415926535897;
   Rhxy(i) = 0.2 * (1/frq(i)) * (tf(i,1) * conj(tf(i,1)));
   Phyx(i) = (atan2(imag(tf(i,2)),real(tf(i,2))) * 180 / 3.1415926535897);
   Rhyx(i) = 0.2 * (1/frq(i)) * (tf(i,2) * conj(tf(i,2)));
end;

k=find(Phyx>-180&Phyx<-90);
Phyx(k)=Phyx(k)+180;
k=find(Phxy<-180&Phxy<-90);
Phxy(k)=Phxy(k)+180;

k=find(Phyx>-90&Phyx<-0);
Phyx(k)=Phyx(k)+90;
k=find(Phxy>-90&Phxy<-0);
Phxy(k)=Phxy(k)+90;


PhxyErr = (tf(:,5)./abs(tf(:,1)))*(180/pi); 
PhyxErr = (tf(:,6)./abs(tf(:,2)))*(180/pi);
RhxyErr = (tf(:,5).*abs(tf(:,1))./frq') * 0.4; % bit confusion. may be ok -dt 2.8.2
RhyxErr = (tf(:,6).*abs(tf(:,2))./frq') * 0.4;

%--------------------------------------------------------------
T = 1./frq;

fid=fopen('e:\manoj\projects\satyam\work.txt','wt');
fprintf(fid,'Site>%s\n',ProcDef.file1);
fprintf(fid,'ACQBY>NGRI\n');
fprintf(fid,'LAT>+021:16:00\n');
fprintf(fid,'LONG>+077:22:43\n');
fprintf(fid,'ACQDATE>14/03/00\n');
fprintf(fid,'Frequency       Rho-XY	Err	Phase-XY Err	Rho-YX	Err	Phase-YX Err\n');
for i = 1:nfrq,
   fprintf(fid,'%5.3e,%5.3e,%5.3e,%5.3e,%5.3e,%5.3e,%5.3e,%5.3e,%5.3e\n',frq(i),Rhxy(i),RhxyErr(i),Phxy(i),...
      PhxyErr(i),Rhyx(i),RhyxErr(i),Phyx(i),PhyxErr(i));
end;

fprintf(fid,'\nXY>>\nRES> 17 11 2.6 25000 8.6 86\nTHICK> 84 600 1500 2400 1600\n\nYX>>\nRES> 13 3 13 21000 7.5 180\nTHICK> 720 1240 2000 9500 3100\n');
fclose(fid);
